<?php
namespace Shopmanage\Controller;
use Think\Controller;
class KamiController extends ExtendController {
	public function __construct() {
		parent::__construct();
		$this->assign("current","kami");
	}
	public function index(){
		Vendor('Page');
		$ctp=I("get.ctp");
		$stp=I("stp");
		$serch=I("serch");
		$ctp=$ctp?$ctp:"all";
		$tools = array(
			"all"=>array("name"=>"全部","status"=>1,"id"=>10,"tip"=>"填写商品地址"),
			"ingift"=>array("name"=>"未中奖","status"=>1,"id"=>8,"tip"=>"填写商品地址"),
			"send"=>array("name"=>"已发奖","status"=>1,"id"=>9,"tip"=>"填写商品地址")
		);
		$where = "`kami`.`sid`=".$this->sid;
		if($stp==1){
			$where .= " AND `smuser`.`usernick`='".$serch."'";
		}elseif($stp==0){
			$where .= " AND `kami`.`cardno`='".$serch."'";
		}else{
			if($ctp=="all"||!$ctp){
				$where.=" AND `kami`.`status`>=8";
			}else{
				if($ctp=="crash"){
					$where.=" AND `kami`.`status`<8";
				}else{
					$where.=" AND `kami`.`status`=".$tools[$ctp]["id"];
				}
			}
		}
		$KM = D("KamiView");
		$total = $KM->where($where)->count();
		
		$Page = new \Page($total,$this->listRows);
		$Page->parameter["serch"] = $serch;
		$Page->rollPage=$this->rollPage;
		$show = $Page->show();
		
		$data = $KM->where($where)
				  ->limit($Page->firstRow.','.$Page->listRows)
				  ->order("id desc")
				  ->getField("id,nick,cardno,secret,giftid,status,category,shopuid,tscode,giftname");
		foreach($data as $k=>$gift){
			foreach($tools as $tool){
				if($data[$k]["status"]==$tool["id"]){
					$data[$k]["status_name"]=$tool["name"];
				}
			}
			switch($data[$k]["status"]){
				case 0:$data[$k]["status_name"]="未保存";break;
				case 1:$data[$k]["status_name"]="已丢弃";break;
				case 2:$data[$k]["status_name"]="已删除";break;
			}
			if(!$data[$k]["nick"]){
				$data[$k]["nick"]="";
			}
		}
		$info["type"]="info";
		$info["msg"]="此处仅查看和管理卡密，添加奖品时才可添加卡密。";
		if($ctp=="crash"){
			$info["type"]="warning";
			$info["msg"]="回收站的卡密会在3天内清空";
		}
		$this->assign('totalpg',(int)$Page->totalPages);
		$this->assign('curpg',$Page->nowPage);
		$this->assign('search',$serch);
		$this->assign('data',$data);
		$this->assign('page',$show);
		$this->assign('stp',$stp);
		$this->assign('ctp',$ctp);
		$this->assign('tools',$tools);
		$this->assign('info',$info);
		$this->assign('title','卡密管理');
		$this->display();
	}
	public function loadbytscode(){
		$tscode = I("tscode");
		$isall = I("isall");
		if(!$isall){
			$where = " AND `status`<=8";
		}else{
			$where = " AND `status`=9";
		}
		$KM = M("EventsGiftsKm");
		$km["allcount"] = $KM->where("`tscode`='".$tscode."' AND `sid`=".$this->sid)->count("`id`");
		$km["count"] = $KM->where("`tscode`='".$tscode."' AND `sid`=".$this->sid." AND `status`<=8")->count("`id`");
		$km["data"] = $KM->where("`tscode`='".$tscode."' AND `sid`=".$this->sid.$where)->getField("id,cardno,secret,giftid,status,shopuid");
		$this->ajaxReturn($km);
	}
	public function add(){
		$vars = I("km");
		if($vars){
			$vars = explode("\n",$vars);
			$ts = time().rand(100,999);
				foreach($vars as $var){
					$tmp = explode(",",$var);
					$da=array();
					if($tmp[1]){
						$da["tscode"]=$ts;
						$da["cardno"]=$tmp[0];
						$da["secret"]=$tmp[1];
						$da["sid"]=$this->sid;
						$data[]=$da;
					}
				}
				$KM = M("EventsGiftsKm");
				$status["code"] = $KM->addAll($data);
				if($status["code"]){
					$status["msg"]="成功保存卡密！已保存".count($data)."个卡密";
					$status["tscode"]=$ts;
				}else{
					if(count($data)==0){
						$msg = "可保存的卡密为0个，请确认卡密的格式。";
					}
					$status["msg"]="保存卡密失败。。。".$msg;
				}
		}else{
			$status["code"]=false;
			$status["msg"]="您没有输入卡密，请重新输入！";
		}
		$this->ajaxReturn($status);
	}
}